package com.avic.rong.goodsSort.activity.secondListActivity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.goodsSort.CarView;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.goodsSort.activity.MaxRecyclerView;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsDrawerChuBanSheRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsDrawerDateRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsDrawerFenLeiRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsShowRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsSxShowRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsTimeShowRvAdapter;
import com.avic.rong.goodsSort.api.secondListApi.GoodsSortSearchAllTsApi;
import com.avic.rong.goodsSort.api.secondListApi.GoodsSortSearchAllTsSxApi;
import com.avic.rong.goodsSort.bean.GoodsSortDrawerBean;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllTsBean;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllTsTimeBean;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchTsSxBean;
import com.avic.rong.search.api.SearchTsAttributeApi;
import com.avic.rong.search.bean.SearchTsAttributeBean;
import com.avic.rong.shoppingCar.activity.ShoppingCarActivity;
import com.avic.rong.shoppingCar.api.ShoppingCarGoodsKuCunApi;
import com.avic.rong.shoppingCar.api.ShoppingCarListApi;
import com.avic.rong.shoppingCar.bean.ShoppingCarListBean;
import com.avic.rong.userOrder.api.AddCarApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DateUtils;
import com.avic.rong.utils.DeviceUtils;
import com.avic.rong.utils.EditUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.StringUtil;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsSortTsActivity extends BaseActivity implements View.OnClickListener, GoodsSortTsDrawerChuBanSheRvAdapter.GoodsSortTsDrawerChuBanSheListener,
        GoodsSortTsDrawerDateRvAdapter.GoodsSortTsDrawerDateListener, GoodsSortTsDrawerFenLeiRvAdapter.GoodsSortTsDrawerFenLeiListener,
        GoodsSortTsShowRvAdapter.GoodsTsRvListener, GoodsSortTsTimeShowRvAdapter.GoodsTsTimeRvListener, GoodsSortTsSxShowRvAdapter.GoodsTsSxRvListener {


    @BindView(R.id.goods_sort_ts_back)
    ImageView goodsSortTsBack;
    @BindView(R.id.goods_sort_ts_fenlei)
    ImageView goodsSortTsFenlei;
    @BindView(R.id.goods_sort_ts_edit)
    EditText goodsSortTsEdit;
    @BindView(R.id.goods_sort_ts_title)
    RelativeLayout goodsSortTsTitle;
    @BindView(R.id.goods_sort_ts_zh)
    RadioButton goodsSortTsZh;
    @BindView(R.id.goods_sort_ts_time)
    RadioButton goodsSortTsTime;
    @BindView(R.id.goods_sort_ts_hot)
    RadioButton goodsSortTsHot;
    @BindView(R.id.goods_sort_ts_shaixuan)
    RadioButton goodsSortTsShaixuan;
    @BindView(R.id.goods_sort_ts_sort)
    LinearLayout goodsSortTsSort;
    @BindView(R.id.goods_sort_ts_fenge)
    View goodsSortTsFenge;
    @BindView(R.id.goods_sort_ts_Rv)
    MaxRecyclerView goodsSortTsRv;
    @BindView(R.id.goods_sort_ts_drawer_fenlei_tv)
    TextView goodsSortTsDrawerFenleiTv;
    @BindView(R.id.goods_sort_ts_drawer_fenlei_rv)
    RecyclerView goodsSortTsDrawerFenleiRv;
    @BindView(R.id.goods_sort_ts_drawer_chubanshe_tv)
    TextView goodsSortTsDrawerChubansheTv;
    @BindView(R.id.goods_sort_ts_drawer_chubanshe_rv)
    RecyclerView goodsSortTsDrawerChubansheRv;
    @BindView(R.id.goods_sort_ts_drawer_author_edit)
    EditText goodsSortTsDrawerAuthorEdit;
    @BindView(R.id.goods_sort_ts_drawer_chubantime_start)
    TextView goodsSortTsDrawerChubantimeStart;
    @BindView(R.id.goods_sort_ts_drawer_chubantime_end)
    TextView goodsSortTsDrawerChubantimeEnd;
    @BindView(R.id.goods_sort_ts_drawer_chubantime_rv)
    RecyclerView goodsSortTsDrawerChubantimeRv;
    @BindView(R.id.goods_sort_ts_drawer_Reset)
    TextView goodsSortTsDrawerReset;
    @BindView(R.id.goods_sort_ts_drawer_Sure)
    TextView goodsSortTsDrawerSure;
    @BindView(R.id.goods_sort_ts_drawer)
    ConstraintLayout goodsSortTsDrawer;
    @BindView(R.id.goods_sort_ts_DrawerLayout)
    DrawerLayout goodsSortTsDrawerLayout;
    @BindView(R.id.search_ts_SmartRefreshLayout)
    SmartRefreshLayout searchTsSmartRefreshLayout;
    @BindView(R.id.goods_sort_ts_LoadingLayout)
    LoadingLayout goodsSortTsLoadingLayout;
    private List<GoodsSortSearchAllTsBean.DataBean> beanList = new ArrayList<>();
    private List<GoodsSortSearchAllTsTimeBean.DataBean> timeList = new ArrayList<>();
    private List<GoodsSortSearchTsSxBean.DataBean> tsSxList = new ArrayList<>();
    private GoodsSortTsShowRvAdapter showRvAdapter;
    private GoodsSortTsTimeShowRvAdapter showTimeRvAdapter;
    private GoodsSortTsSxShowRvAdapter showSxRvAdapter;

    private boolean timeFlag = false;
    private GoodsSortTsDrawerFenLeiRvAdapter tsDrawerFenLeiRvAdapter;
    private GoodsSortTsDrawerChuBanSheRvAdapter tsDrawerChuBanSheRvAdapter;
    private GoodsSortTsDrawerDateRvAdapter tsDrawerDateRvAdapter;
    private List<SearchTsAttributeBean.BookCatBean> tsTsFenLeiDrawerList = new ArrayList<>();
    private ArrayList<GoodsSortDrawerBean> timeDrawerList = new ArrayList<>();
    private ArrayList<GoodsSortDrawerBean> chubansheDrawerList = new ArrayList();
    private CarView carView;
    private TextView carTv;
    private String keyword;
    private GoodsSortSearchAllTsApi searchAllApi;
    private int pageNo = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_sort_ts);
        ButterKnife.bind(this);
        goodsSortTsRv.setNestedScrollingEnabled(false);
        goodsSortTsDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        goodsSortTsZh.setSelected(true);
        Intent intent = getIntent();
        keyword = intent.getStringExtra("keyword");
        goodsSortTsEdit.setText(keyword);
        initListener();
        setAdapter();
        initTsDrawerData();
        initData(keyword);
        //加载购物车商品数量
        loadData();
        carView = new CarView(this);
        carTv = carView.findViewById(R.id.car_number);
        carView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsSortTsActivity.this, ShoppingCarActivity.class);
                startActivity(intent);
            }
        });
        carView.show();
        goodsSortTsLoadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                HttpManager.getInstance().doHttpDeal(searchAllApi);
                goodsSortTsLoadingLayout.setStatus(LoadingLayout.LOADING);
            }

            @Override
            public void onReturn(View v) {
                onBackPressed();
            }
        });
        goodsSortTsLoadingLayout.setStatus(LoadingLayout.LOADING);
    }

    @Override
    protected void onResume() {
        loadData();
        super.onResume();
    }

    private void setAdapter() {
        GoodsSortDrawerBean timeBean1 = new GoodsSortDrawerBean("近一个月", 95 + "", false, true);
        GoodsSortDrawerBean timeBean2 = new GoodsSortDrawerBean("近三个月", 95 + "", false, true);
        GoodsSortDrawerBean timeBean3 = new GoodsSortDrawerBean("近半年", 95 + "", false, true);
        GoodsSortDrawerBean timeBean4 = new GoodsSortDrawerBean("近一年", 95 + "", false, true);
        timeDrawerList.add(timeBean1);
        timeDrawerList.add(timeBean2);
        timeDrawerList.add(timeBean3);
        timeDrawerList.add(timeBean4);

        GridLayoutManager tsFenLeiManager = new GridLayoutManager(this, 3);
        goodsSortTsDrawerFenleiRv.setLayoutManager(tsFenLeiManager);
        tsDrawerFenLeiRvAdapter = new GoodsSortTsDrawerFenLeiRvAdapter(this);
        goodsSortTsDrawerFenleiRv.setAdapter(tsDrawerFenLeiRvAdapter);
        tsDrawerFenLeiRvAdapter.setGoodsSortBzDrawerFenLeiListener(this);

        GridLayoutManager tsChuBanSheManager = new GridLayoutManager(this, 2);
        goodsSortTsDrawerChubansheRv.setLayoutManager(tsChuBanSheManager);
        tsDrawerChuBanSheRvAdapter = new GoodsSortTsDrawerChuBanSheRvAdapter(this);
        goodsSortTsDrawerChubansheRv.setAdapter(tsDrawerChuBanSheRvAdapter);
        tsDrawerChuBanSheRvAdapter.setGoodsSortBzDrawerStateListener(this);

        GridLayoutManager tsDateManager = new GridLayoutManager(this, 3);
        goodsSortTsDrawerChubantimeRv.setLayoutManager(tsDateManager);
        tsDrawerDateRvAdapter = new GoodsSortTsDrawerDateRvAdapter(this);
        goodsSortTsDrawerChubantimeRv.setAdapter(tsDrawerDateRvAdapter);
        tsDrawerDateRvAdapter.setGoodsSortBzDrawerDateListener(this);
    }

    private void initData(String keyword) {
        searchAllApi = new GoodsSortSearchAllTsApi(this, searchAllListener, 1, 2, "1", keyword, pageNo, pageSize);
        searchAllApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(searchAllApi);
    }

    //获取图书数据监听
    HttpOnNextListener<BaseResultEntity> searchAllListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                ToastTools.showToast(GoodsSortTsActivity.this, subjects.getMsg());
                Log.e("cjx", "图书数据：" + subjects.getMsg());
                return;
            }
            ArrayList<GoodsSortSearchAllTsBean> beans = (ArrayList<GoodsSortSearchAllTsBean>) subjects.getData();
            subjects.getData();
            Log.e("cjx", "goodsTsList:" + beans.get(1).getData().size());
            beanList.addAll(beans.get(1).getData());
            //主界面数据
            goodsSortTsRv.setLayoutManager(new LinearLayoutManager(GoodsSortTsActivity.this));
            showRvAdapter = new GoodsSortTsShowRvAdapter(GoodsSortTsActivity.this);
            goodsSortTsRv.setAdapter(showRvAdapter);
            showRvAdapter.setGoodsTsRvListener(GoodsSortTsActivity.this);
            showRvAdapter.setBeanList(beanList);
            goodsSortTsLoadingLayout.setStatus(LoadingLayout.SUCCESS);
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "bz拿到数据" + e.getMessage());
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                goodsSortTsLoadingLayout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                goodsSortTsLoadingLayout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

    private void initTsDrawerData() {
        SearchTsAttributeApi goodsSortApi = new SearchTsAttributeApi(this, delCartListener);
        goodsSortApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(goodsSortApi);
    }

    //获取图书属性数据监听
    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                Log.e("cjx", "图书属性:" + subjects.getData());
                return;
            }
            SearchTsAttributeBean bean = (SearchTsAttributeBean) subjects.getData();
            Log.e("cjx", "图书属性拿到数据");
            int attrId = bean.getAllAttrList().get(0).getAttrId();
            tsTsFenLeiDrawerList.addAll(bean.getBookCat());
            tsDrawerFenLeiRvAdapter.setTsTsFenLeiDrawerList(tsTsFenLeiDrawerList);
            List<SearchTsAttributeBean.AllAttrListBean> allAttrDrawerList = bean.getAllAttrList();
            List<String> attrValuesList = allAttrDrawerList.get(0).getAttrValues();
            for (int i = 0; i < attrValuesList.size(); i++) {
                String s = attrValuesList.get(i);
                GoodsSortDrawerBean goodsSortDrawerBean = new GoodsSortDrawerBean(s, attrId + "", false, true);
                chubansheDrawerList.add(goodsSortDrawerBean);
            }
            tsDrawerChuBanSheRvAdapter.setTsTsFenLeiDrawerList(chubansheDrawerList);

            tsDrawerDateRvAdapter.setTsDrawerDateList(timeDrawerList);
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };


    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void addListener() {

    }

    private void initListener() {
        goodsSortTsBack.setOnClickListener(this);
        goodsSortTsEdit.setOnClickListener(this);
        goodsSortTsFenlei.setOnClickListener(this);

        goodsSortTsZh.setOnClickListener(this);
        goodsSortTsTime.setOnClickListener(this);
        goodsSortTsHot.setOnClickListener(this);
        goodsSortTsShaixuan.setOnClickListener(this);

        goodsSortTsDrawerFenleiTv.setOnClickListener(this);
        goodsSortTsDrawerChubansheTv.setOnClickListener(this);
        goodsSortTsDrawerChubantimeStart.setOnClickListener(this);
        goodsSortTsDrawerChubantimeEnd.setOnClickListener(this);

        goodsSortTsDrawerReset.setOnClickListener(this);
        goodsSortTsDrawerSure.setOnClickListener(this);

        goodsSortTsEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //关闭软键盘
                    EditUtils.showORhideSoftKeyboard(GoodsSortTsActivity.this);
                    keyword = goodsSortTsEdit.getText().toString();
                    beanList.clear();
                    goodsSortTsZh.setSelected(true);
                    goodsSortTsTime.setSelected(false);
                    goodsSortTsHot.setSelected(false);
                    goodsSortTsShaixuan.setSelected(false);
                    initData(keyword);
                    showRvAdapter.notifyDataSetChanged();
                    return true;
                }
                return false;
            }
        });
        searchTsSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                beanList.clear();
                initData(keyword);
                searchTsSmartRefreshLayout.finishRefresh(1000);
            }
        });
        searchTsSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                ++pageNo;
                initData(keyword);
                searchTsSmartRefreshLayout.finishLoadMore(1000);
            }
        });
    }

    private void setSortTimeDrawable(int p) {
        Drawable downTime = getResources().getDrawable(p);
        downTime.setBounds(0, 0, downTime.getMinimumWidth(), downTime.getMinimumHeight());
        goodsSortTsTime.setCompoundDrawables(null, null, downTime, null);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.goods_sort_ts_back:
                GoodsSortTsActivity.this.finish();
                break;
            case R.id.goods_sort_ts_edit:
                break;
            case R.id.goods_sort_ts_fenlei:
                Intent intent = new Intent(this, GoodsSortActivity.class);
                startActivity(intent);
                this.finish();
                break;
            case R.id.goods_sort_ts_zh:
                goodsSortTsZh.setSelected(true);
                goodsSortTsTime.setSelected(false);
                goodsSortTsHot.setSelected(false);
                goodsSortTsShaixuan.setSelected(false);
                setSortTimeDrawable(R.mipmap.goods_sort_time_sort);
                setTsShowRvListClear();
                initData(keyword);
                showRvAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_ts_time:
                goodsSortTsZh.setSelected(false);
                goodsSortTsTime.setSelected(true);
                goodsSortTsHot.setSelected(false);
                goodsSortTsShaixuan.setSelected(false);
                setTsShowRvListClear();
                if (timeFlag) {
                    initTimeData(0, keyword);
                    setSortTimeDrawable(R.mipmap.down_time_sort2x);
                } else {
                    initTimeData(1, keyword);
                    setSortTimeDrawable(R.mipmap.up_time_sort2x);
                }
                timeFlag = !timeFlag;
                break;
            case R.id.goods_sort_ts_hot:
                goodsSortTsZh.setSelected(false);
                goodsSortTsTime.setSelected(false);
                goodsSortTsHot.setSelected(true);
                goodsSortTsShaixuan.setSelected(false);
                setSortTimeDrawable(R.mipmap.goods_sort_time_sort);
                setTsShowRvListClear();
                initData(keyword);
                showRvAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_ts_shaixuan:
//                setTsDrawerListClear();
                goodsSortTsZh.setSelected(false);
                goodsSortTsTime.setSelected(false);
                goodsSortTsHot.setSelected(false);
                goodsSortTsShaixuan.setSelected(true);
                setSortTimeDrawable(R.mipmap.goods_sort_time_sort);
                goodsSortTsDrawerLayout.openDrawer(GravityCompat.END);
                break;
            case R.id.goods_sort_ts_drawer_fenlei_tv:
                if (tsDrawerFenLeiRvAdapter.getItemCount() == 6) {
                    tsDrawerFenLeiRvAdapter.addItemNums(tsTsFenLeiDrawerList.size());
                    tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
                    goodsSortTsDrawerFenleiTv.setText("收回  ");
                } else {
                    tsDrawerFenLeiRvAdapter.addItemNums(6);
                    tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
                    goodsSortTsDrawerFenleiTv.setText("全部  ");
                }
                tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_ts_drawer_chubanshe_tv:
                if (tsDrawerChuBanSheRvAdapter.getItemCount() == 6) {
                    tsDrawerChuBanSheRvAdapter.addItemNums(chubansheDrawerList.size());
                    tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
                } else {
                    tsDrawerChuBanSheRvAdapter.addItemNums(6);
                    tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
                    goodsSortTsDrawerChubansheTv.setText("全部  ");
                }
                tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_ts_drawer_chubantime_start:
                View view = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop(view);
                break;
            case R.id.goods_sort_ts_drawer_chubantime_end:
                View view1 = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop1(view1);
                break;
            case R.id.goods_sort_ts_drawer_Reset:
                goodsSortTsDrawerFenleiTv.setText("全部  ");
                goodsSortTsDrawerChubansheTv.setText("全部  ");
                goodsSortTsDrawerChubantimeStart.setText("起始日期");
                goodsSortTsDrawerChubantimeEnd.setText("截止日期");
                for (int i = 0; i < tsTsFenLeiDrawerList.size(); i++) {
                    tsTsFenLeiDrawerList.get(i).setSelected(false);
                }
                tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
                for (int i = 0; i < chubansheDrawerList.size(); i++) {
                    chubansheDrawerList.get(i).setFlag(false);
                }
                tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
                for (int i = 0; i < timeDrawerList.size(); i++) {
                    timeDrawerList.get(i).setFlag(false);
                }
                tsDrawerDateRvAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_ts_drawer_Sure:
                setTsShowRvListClear();
                initTsSureData();
                goodsSortTsDrawerLayout.closeDrawer(GravityCompat.END);
                break;
        }
    }

    private void initTsSureData() {
        String attrPro = getTsNowAttrPro();
        Log.e(TAG, "initTsSureData: " + attrPro);
        GoodsSortSearchAllTsSxApi searchAllSxApi = new GoodsSortSearchAllTsSxApi(this, searchAllTsSxListener, 1, 2, attrPro);
        searchAllSxApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(searchAllSxApi);
    }

    //获取图书时间数据
    HttpOnNextListener<BaseResultEntity> searchAllTsSxListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                ToastTools.showToast(GoodsSortTsActivity.this, subjects.getMsg());
                Log.e("cjx", "图书time数据：" + subjects.getMsg());
                return;
            }
            ArrayList<GoodsSortSearchTsSxBean> beans = (ArrayList<GoodsSortSearchTsSxBean>) subjects.getData();
            tsSxList.addAll(beans.get(1).getData());
            Log.e("cjx", "ts time数据长度：" + timeList.size() + "");
            //主界面数据
            goodsSortTsRv.setLayoutManager(new LinearLayoutManager(GoodsSortTsActivity.this));
            showSxRvAdapter = new GoodsSortTsSxShowRvAdapter(GoodsSortTsActivity.this);
            goodsSortTsRv.setAdapter(showSxRvAdapter);
            showSxRvAdapter.setGoodsTsSxListener(GoodsSortTsActivity.this);
            showSxRvAdapter.setTsSxList(tsSxList);
            showSxRvAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "ts timelist拿到数据" + e.getMessage());
        }
    };

    private String getTsNowAttrPro() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < tsTsFenLeiDrawerList.size(); i++) {
            if (tsTsFenLeiDrawerList.get(i).isSelected()) {
                sb.append(tsTsFenLeiDrawerList.get(i).getCatId() + "__" + tsTsFenLeiDrawerList.get(i).getCatName());
            }
        }
        for (int i = 0; i < chubansheDrawerList.size(); i++) {
            if (chubansheDrawerList.get(i).isFlag()) {
                sb.append("&95__" + chubansheDrawerList.get(i).getS());
            }
        }
        String author = goodsSortTsDrawerAuthorEdit.getEditableText().toString();
        if (!author.equals("")) {
            sb.append("&91__" + author);
        }
        if (goodsSortTsDrawerChubantimeStart.getText().toString().equals("起始日期") && goodsSortTsDrawerChubantimeEnd.getText().toString().equals("截止日期")) {
            for (int i = 0; i < timeDrawerList.size(); i++) {
                if (timeDrawerList.get(i).isFlag()) {
                    if (timeDrawerList.get(i).getS().equals("近一个月")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(30);
                        sb.append("&start93__" + beforeDateTime);
                    } else if (timeDrawerList.get(i).getS().equals("近三个月")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(90);
                        sb.append("&start93__" + beforeDateTime);
                    } else if (timeDrawerList.get(i).getS().equals("近半年")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(180);
                        sb.append("&start93__" + beforeDateTime);
                    } else if (timeDrawerList.get(i).getS().equals("近一年")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(365);
                        sb.append("&start93__" + beforeDateTime);
                    }
                    String nowDateTime = DateUtils.getNowDateTime();
                    sb.append("&end93__" + nowDateTime);
                }
            }
        } else {
            sb.append("&start93__" + goodsSortTsDrawerChubantimeStart.getText().toString());
            sb.append("&end93__" + goodsSortTsDrawerChubantimeEnd.getText().toString());
        }
        Log.e("cjx", "图书拼接属性：" + sb.toString());
        return sb.toString();
    }

    private void setTsShowRvListClear() {
        beanList.clear();
        timeList.clear();
        tsSxList.clear();
    }

    private void setTsDrawerListClear() {
        tsTsFenLeiDrawerList.clear();
        timeDrawerList.clear();
        chubansheDrawerList.clear();
    }

    //获取图书时间数据监听
    private void initTimeData(int i, String keyword) {
        GoodsSortSearchAllTsApi searchAllApi = new GoodsSortSearchAllTsApi(this, searchAllTsTimeListener, 1, 2, i, "2", keyword);
        searchAllApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(searchAllApi);
    }

    //获取图书时间数据
    HttpOnNextListener<BaseResultEntity> searchAllTsTimeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                ToastTools.showToast(GoodsSortTsActivity.this, subjects.getMsg());
                Log.e("cjx", "图书time数据：" + subjects.getMsg());
                return;
            }
            ArrayList<GoodsSortSearchAllTsTimeBean> beans = (ArrayList<GoodsSortSearchAllTsTimeBean>) subjects.getData();
            timeList.addAll(beans.get(1).getData());
            Log.e("cjx", "ts time数据长度：" + timeList.size() + "");
            //主界面数据
            goodsSortTsRv.setLayoutManager(new LinearLayoutManager(GoodsSortTsActivity.this));
            showTimeRvAdapter = new GoodsSortTsTimeShowRvAdapter(GoodsSortTsActivity.this);
            goodsSortTsRv.setAdapter(showTimeRvAdapter);
            showTimeRvAdapter.setGoodsTsTimeRvListener(GoodsSortTsActivity.this);
            showTimeRvAdapter.setTimeList(timeList);
            showTimeRvAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "ts timelist拿到数据" + e.getMessage());
        }
    };

    //时间选择弹窗
    private void commonPop(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_ts_drawer), Gravity.BOTTOM, 0, 0);
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortTsDrawerChubantimeStart.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop1(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_ts_drawer), Gravity.BOTTOM, 0, 0);
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortTsDrawerChubantimeEnd.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //出版社
    @Override
    public void onTsDrawerChuBanSheSelected(int index) {
        for (int i = 0; i < chubansheDrawerList.size(); i++) {
            if (i == index) {
                chubansheDrawerList.get(index).setFlag(!chubansheDrawerList.get(i).isFlag());
                continue;
            }
        }
        StringBuffer sb = new StringBuffer();
        for (int j = 0; j < chubansheDrawerList.size(); j++) {
            if (chubansheDrawerList.get(j).isFlag()) {
                sb.append(chubansheDrawerList.get(j).getS() + "丶");
            }
        }
        Log.e("cjx", "chubansheFlag:" + chubansheDrawerList.get(index).isFlag());
        if (StringUtil.isEmpty(sb.toString())) {
            goodsSortTsDrawerChubansheTv.setText("全部  ");
        } else {
            goodsSortTsDrawerChubansheTv.setText(sb.toString() + "  ");
        }
        tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
    }

    //日期
    @Override
    public void onTsDrawerDateSelected(int index) {
        for (int i = 0; i < timeDrawerList.size(); i++) {
            if (i == index) {
                timeDrawerList.get(i).setFlag(!timeDrawerList.get(i).isFlag());
            } else {
                timeDrawerList.get(i).setFlag(false);
            }
        }
        tsDrawerDateRvAdapter.notifyDataSetChanged();
    }

    //图书分类属性
    @Override
    public void onTsFenLeiSelected(int index) {
        for (int i = 0; i < tsTsFenLeiDrawerList.size(); i++) {
            if (i == index) {
                tsTsFenLeiDrawerList.get(i).setSelected(!tsTsFenLeiDrawerList.get(i).isSelected());
            } else {
                tsTsFenLeiDrawerList.get(i).setSelected(false);
            }
        }
        String nowSelectText = "";
        for (int j = 0; j < tsTsFenLeiDrawerList.size(); j++) {
            if (tsTsFenLeiDrawerList.get(j).isSelected()) {
                nowSelectText = tsTsFenLeiDrawerList.get(j).getCatName();
            }
        }
        if (StringUtil.isEmpty(nowSelectText)) {
            goodsSortTsDrawerFenleiTv.setText("全部  ");
        } else {
            goodsSortTsDrawerFenleiTv.setText(tsTsFenLeiDrawerList.get(index).getCatName() + "  ");
        }
        tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        carView.destory();
    }

    @Override
    public void addTsCar(String skuStr, GoodsSortTsShowRvAdapter.GoodsSortTsShowRvViewHolder holder) {
        SharedPreferencesUtil.getInstance().put("skuId", skuStr);
        String[] split = skuStr.split(":");
        String substring = split[0].substring(2, split[0].length() - 1);
        String substring2 = split[1].substring(0, split[1].length() - 1);
        getKuCun(Integer.parseInt(substring2), Integer.parseInt(substring));
        addProductionAnimation(holder.car, carView);
    }

    @Override
    public void addTimeTsCar(String skuStr) {
        SharedPreferencesUtil.getInstance().put("skuId", skuStr);
        String[] split = skuStr.split(":");
        String substring = split[0].substring(2, split[0].length() - 1);
        String substring2 = split[1].substring(0, split[0].length() - 2);
        getKuCun(Integer.parseInt(substring2), Integer.parseInt(substring));
    }

    private void AddCar(String skuStr) {
        Log.e("cjx", "skuStr:" + skuStr);
        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            Map<String, String> map = new HashMap<>();
            map.put("skuStr", skuStr);
            map.put("orderType", 4 + "");
//            是否再次购买（0/1）
            map.put("buyAgain", 0 + "");
            map.put("token", getSharedPreferencesUtil().getLogintoken());
            map.put("deviceId", DeviceUtils.getIMEString(context));
            final AddCarApi addCarApi = new AddCarApi(GoodsSortTsActivity.this, simpleOnNextListenerAddCar);
            addCarApi.setCache(false);
            addCarApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(addCarApi);
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("skuStr", skuStr);
            map.put("orderType", 4 + "");
//            是否再次购买（0/1）
            map.put("buyAgain", 0 + "");
            map.put("token", "");
            map.put("deviceId", DeviceUtils.getIMEString(context));
            final AddCarApi addCarApi = new AddCarApi(GoodsSortTsActivity.this, simpleOnNextListenerAddCar);
            addCarApi.setCache(false);
            addCarApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(addCarApi);
        }
    }

    //库存
    HttpOnNextListener httpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            String skuId = SharedPreferencesUtil.getInstance().getAsString("skuId", "");
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                Toast.makeText(context, "商品库存不足", Toast.LENGTH_SHORT).show();
            } else {
                AddCar(skuId);
            }
        }
    };

    private void getKuCun(int num, int skuId) {
        ShoppingCarGoodsKuCunApi goodsKuCunApi = new ShoppingCarGoodsKuCunApi(GoodsSortTsActivity.this, httpOnNextListener, num, skuId);
        goodsKuCunApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(goodsKuCunApi);
    }

    //添加购物车
    HttpOnNextListener simpleOnNextListenerAddCar = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {

            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(context, subjects.getMsg());
                return;
            }
            double currentCarNumber = (double) subjects.getData();
            Log.e("cjx", "currentCarNumber:" + currentCarNumber + "");
            int i = (int) currentCarNumber;
            carTv.setText(i + "");
//            View toastView = LayoutInflater.from(GoodsSortBzActivity.this).inflate(R.layout.toast_addcar_success,null);
//            ToastTools.showToast(context,"加入购物车成功！");
            ToastTools.showToast("");
        }


        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };

    //获取购物车列表数据
    private void loadData() {
        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            final ShoppingCarListApi shoppingCarListApi = new ShoppingCarListApi(this, simpleOnNextListener, SharedPreferencesUtil.getLoginToken(), DeviceUtils.getIMEString(GoodsSortTsActivity.this));
            shoppingCarListApi.setCache(false);
            HttpManager.getInstance().doHttpDeal(shoppingCarListApi);
        } else {
            final ShoppingCarListApi shoppingCarListApi = new ShoppingCarListApi(this, simpleOnNextListener, "", DeviceUtils.getIMEString(GoodsSortTsActivity.this));
            shoppingCarListApi.setCache(false);
            HttpManager.getInstance().doHttpDeal(shoppingCarListApi);
        }
    }

    //获取购物车列表数据监听
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(GoodsSortTsActivity.this, subjects.getMsg());
                Log.e("cjx", subjects.getMsg());
                return;
            }
            ShoppingCarListBean bean = (ShoppingCarListBean) subjects.getData();
            int size = bean.getCartList().size();
            int num = 0;
            for (int i = 0; i < size; i++) {
                int goodsNumber = bean.getCartList().get(i).getGoodsNumber();
                num += goodsNumber;
            }
            Log.e("cjx", "初始数量：" + size + "");
            carTv.setText(num + "");

        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "Bz购物车列表数据：" + e.getMessage());
        }
    };

    @Override
    public void addTsSxCar(String skuStr) {
        SharedPreferencesUtil.getInstance().put("skuId", skuStr);
        String[] split = skuStr.split(":");
        String substring = split[0].substring(2, split[0].length() - 1);
        String substring2 = split[1].substring(0, split[0].length() - 2);
        getKuCun(Integer.parseInt(substring2), Integer.parseInt(substring));
    }
}
